package action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import po.User;
import service.UserService;

public class UserAction {
	//User
	private User loginUser;
	
	private File upload = null;  
    private String uploadContentType;//文件类型,这两个属性，Struts2自动会解析  
    private String uploadFileName;   //
	
	public User getLoginUser() {
		return loginUser;
	}
	public void setLoginUser(User loginUser) {
		this.loginUser = loginUser;
	}
	
	//登陆逻辑
	public String login() {
		UserService userServ = new UserService();
		//管理员登陆
		if(loginUser.getAccount().equals("super")&&loginUser.getPassword().equals("super")) {
//			HttpServletRequest request = ServletActionContext.getRequest();
//			HttpSession session = request.getSession();
//			//int id = userServ.login(loginUser);
//			session.setAttribute("userID",0);
			return "super";
		}
		if (userServ.login(loginUser)!=0) {
			HttpServletRequest request = ServletActionContext.getRequest();
			HttpSession session = request.getSession();
			//int id = userServ.login(loginUser);
			session.setAttribute("userID",userServ.login(loginUser));
			return "success";
		}
			return "fail";
	}
	
	//注册逻辑
	public String register() {
		UserService userServ = new UserService();
		System.out.println(loginUser.getAccount()+" "+loginUser.getUserDetail().getPhone());
		if(userServ.register(loginUser)) {
			return "success";
		}
		return "fail";
	}
	
	public String registerTutor() {
		String destPath = "D:\\TASK\\Tutor platform\\WebContent\\images\\";
		String newFileName =null;
		HttpServletRequest request = ServletActionContext.getRequest();
		//得到用户id 正常登陆取值正常
		HttpSession session = request.getSession();
		int userID = (int) session.getAttribute("userID");
		UserService userServ = new UserService();
		try {
			
			newFileName = userID + uploadFileName;
			File destFile  = new File(destPath, ""+newFileName);

            FileUtils.copyFile(upload, destFile);
           
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		if(userServ.registerTutor(loginUser,userID,"images//"+newFileName)) {
			return "success";
		}else {
			return "fail";
		}
	}

	public File getUpload() {
		return upload;
	}
	public void setUpload(File upload) {
		this.upload = upload;
	}
	public String getUploadContentType() {
		return uploadContentType;
	}
	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}
	public String getUploadFileName() {
		return uploadFileName;
	}
	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
}
